'use strict';

var realTinfoArrList = [];
//模拟加载行业新闻
// function topic() {
//   $.server({ categoryId: 32, pageSize: 6, pageNum: 1 }, '/website/v1/queryCategoryContent').then(function (res) {
//     console.log(res, "行业新闻");
//     var obj = res.result.data;
//     var html = '',
//         length = obj.length > 6 ? 6 : obj.length;
//     for (var i = 0; i < length; i++) {
//       if (i == 0) {
//         if (obj[i].description && obj[i].description.length > 80) {
//           obj[i].description = obj[i].description.substring(0, 80) + "...";
//         }
//         html += ' <a onclick="goLessonDetail(' + obj[i].contentId + ',\'\u7ECF\u6D4E\',\'\u884C\u4E1A\u65B0\u95FB\')">\n        <div class="new-title-box col-md-12 col-sm-12 col-xs-12">\n            <div class="new-title">' + obj[i].title + '</div>\n            <div class="new-content-box">\n                <img class="new-img img-responsive" src="' + obj[i].poster + '"></img>\n                <div class="new-word">\n                ' + obj[i].description + '\n                </div>\n            </div>\n        </div>\n    </a>';
//       } else {
//         html += '<a onclick="goLessonDetail(' + obj[i].contentId + ',\'\u7ECF\u6D4E\',\'\u884C\u4E1A\u65B0\u95FB\')"><div class="topic-box title-box col-md-12 col-sm-12 col-xs-12">\n      <div class="flex-box">\n      <div class="around"></div>\n      <div class="topic-title">' + obj[i].title + '</div>\n      </div>\n      <div class="more">' + obj[i].createTime.substring(5, 10) + '</div>\n      </div></div>';
//       }
//     }
//     $("#topicContent").html(html);
//   });
// }

//模拟加载学校
// function financeAndEconomics() {
//   $.server({ categoryId: 33, pageSize: 6, pageNum: 1 }, '/website/v1/queryCategoryContent').then((res) => {
//     console.log(res, "学校")
//     let obj = res.result.data
//     let html = '', length = obj.length > 8 ? 8 : obj.length;
//     for (let i = 0; i < length; i++) {
//       html += `<div class="coolleges-w-box col-md-3 col-sm-3 col-xs-3">
//       <img src=${obj[i].poster} class="coolleges-img img-responsive" alt="">
//       <div class="coolleges-name">${obj[i].title}</div>
//   </div>`
//     }
//     $("#financeAndEconomics").html(html);
//   })
// }
//模拟加载学校
// function financeAndEconomics() {
//   var obj = [{ title: '中国商业健康保险论坛', img: 'images/financeAndEconomics/financeAndEconomics-3.png', id: '344' }, { title: '科技创新高峰论坛', img: 'images/financeAndEconomics/financeAndEconomics-4.png', id: '344' }, { title: '中国保险业发展年会', img: 'images/financeAndEconomics/financeAndEconomics-5.png', id: '344' }, { title: '中国保险营销峰会', img: 'images/financeAndEconomics/financeAndEconomics-6.png', id: '344' }, { title: '中国商业健康保险论坛', img: 'images/financeAndEconomics/financeAndEconomics-7.png', id: '344' }, { title: '科技创新高峰论坛', img: 'images/financeAndEconomics/financeAndEconomics-8.png', id: '344' }, { title: '中国保险业发展年会', img: 'images/financeAndEconomics/financeAndEconomics-9.png', id: '344' }, { title: '中国保险营销峰会', img: 'images/financeAndEconomics/financeAndEconomics-10.png', id: '344' }];
//   var html = '',
//       length = obj.length > 10 ? 10 : obj.length;

//   for (var i = 0; i < length; i++) {
//     html += '<div class="coolleges-w-box col-md-3 col-sm-3 col-xs-3">\n    <img src=' + obj[i].img + ' class="coolleges-img img-responsive" alt="">\n    <div class="coolleges-name">' + obj[i].title + '</div>\n</div>';
//   }
//   $("#financeAndEconomics").html(html);
// }
// //模拟专题报告
// function researchFindings() {
//   $.server({ categoryId: 33, pageSize: 6, pageNum: 1 }, '/website/v1/queryCategoryContent').then(function (res) {
//     console.log(res, "专题报告");
//     var obj = res.result.data;
//     var html = '',
//         length = obj.length > 6 ? 6 : obj.length;
//     for (var i = 0; i < length; i++) {
//       if (i == 0) {
//         if (obj[i].description && obj[i].description.length > 80) {
//           obj[i].description = obj[i].description.substring(0, 80) + "...";
//         }
//         html += ' <a onclick="goLessonDetail(' + obj[i].contentId + ',\'\u7ECF\u6D4E\',\'\u4E13\u9898\u62A5\u9053\')">\n        <div class="new-title-box col-md-12 col-sm-12 col-xs-12">\n            <div class="new-title">' + obj[i].title + '</div>\n            <div class="new-content-box">\n                <img class="new-img img-responsive" src="' + obj[i].poster + '"></img>\n                <div class="new-word">\n                ' + obj[i].description + '\n                </div>\n            </div>\n        </div>\n    </a>';
//       } else {
//         html += '<a onclick="goLessonDetail(' + obj[i].contentId + ',\'\u7ECF\u6D4E\',\'\u4E13\u9898\u62A5\u9053\')"><div class="topic-box title-box col-md-12 col-sm-12 col-xs-12">\n      <div class="flex-box">\n      <div class="around"></div>\n      <div class="topic-title">' + obj[i].title + '</div>\n      </div>\n      <div class="more">' + obj[i].createTime.substring(5, 10) + '</div>\n      </div></div>';
//       }
//     }
//     $("#researchFindings").html(html);
//   });
// }
function more() {
  var id = 32;
  location.href = 'articleList.html?id=' + id + '&title=经济&text=资讯';
}
function more1() {
  var id = 36;
  location.href = 'articleList.html?id=' + id + '&title=经济&text=今日焦点';
}
// 页面顶部大图数据(待定)
function posterImg() {
  $.server({ location: 4 }, '/website/v1/getPoster').then(function (res) {
    console.log(res, "页面顶部大图数据");
    $('#bannerImg').attr("src", res.result.data[0].url);
  });
}
/**
 * 跳转到文章详情
 */
function goLessonDetail(id, word, childTitle) {
  location.href = 'industryDetail.html?id=' + id + '&word=' + word + '&childTitle=' + childTitle;
}

// 轮播
// 资讯列表数据
function realTinfo() {
  $.server({ navigationType : 2}, '/website/v1/hotContent').then(function (res) {
    console.log(res, "资讯列表");
    realTinfoArrList = res.result;
    $('#newsImageIns').html(realTinfoArrList[0].title);
    realTinfoRendering(realTinfoArrList);
    lunbo(realTinfoArrList);
  });
}

//资讯渲染
function realTinfoRendering(arr) {
  if (!arr) {
    $('#realTinfoRendering').html('');
    return false;
  }
  var html = '';
  for (var i = 0; i < arr.length; i++) {
    html += ' <div  class="swiper-slide swiper-slide-reset" onclick="goNewsDetail(' + arr[i].contentId + ',\'\u9996\u9875\',\'\u8981\u95FB\u680F\u76EE\')"><img style="width:100%" src="' + arr[i].poster + '" alt=""></div>';
    $('#realTinfoRendering').html(html);
  }
}
function lunbo(realTinfoArrList) {
  console.log("2");
  // 上面的大图轮播
  var swiper1 = new Swiper('#swiper1', {
    autoplay: true, //自动切换
    observer: true, //修改swiper自己或子元素时，自动初始化swiper
    observeParents: true, //修改swiper的父元素时，自动初始化swiper
    autoplayDisableOnInteraction: false, //解决手动滑动后不会自己轮播问题
    on: {
      slideChangeTransitionEnd: function slideChangeTransitionEnd() {
        $('#newsImageIns').html(realTinfoArrList[this.activeIndex].title);
        // console.log(this.activeIndex); //切换结束时，告诉我现在是第几个slide
      }
    }
  });
}
function getNewsData(){
  $.server({ categoryId : 31 }, '/website/v1/queryNavCategory').then(function (res) {
    var mainList=res.result.main;
    var rightList=res.result.right;
    loadMain(mainList);
    loadRight(rightList);
  });
}
//加载主新闻
function loadMain(mainList){
  var html='';
  for(var i=0 ; i<mainList.length ; i++){
    if(i==mainList.length-1){
      html+= '<span class="expertSpan " data-index="'+mainList[i].categoryId+'" data-name="'+mainList[i].categoryName+'">'+mainList[i].categoryName+'</span>';
    }else {
      html+= '<span class="expertSpan" data-index="'+mainList[i].categoryId+'" data-name="'+mainList[i].categoryName+'">'+mainList[i].categoryName+'</span><span class="title-c xiegang">/ </span>';
    }
  }
  $(".expert-title-box").html(html);
  $(".expertSpan").eq(0).addClass("title-c");
  $(".expertSpan").click(function(e){
    var index=e.currentTarget.dataset.index;
    var name=e.currentTarget.dataset.name;
    getPageData(index,name);
    $(".expertSpan").removeClass("title-c");
    $(this).addClass("title-c");
  })
  getPageData(mainList[0].categoryId,mainList[0].categoryName);
}
function loadRight(rightList){
  var html='';
  $(".rightList").text(rightList[0].categoryName);
  getPageData(rightList[0].categoryId,rightList[0].categoryName);
  getPageData(rightList[1].categoryId,rightList[1].categoryName);
}

function getPageData(categoryId,name){
  $.server({ categoryId : categoryId }, '/website/v1/queryCategoryContent').then(function (res) {
    var detail=res.result.data;
    if(categoryId==32 || categoryId==33 || categoryId==34){
      var mainListHtml='';
      for(var i=0;i<detail.length;i++){
        mainListHtml+=' <div class="list-box" onclick="goLessonDetail(' + detail[i].contentId + ',\'\u7ECF\u6D4E\',\''+name+'\')">'+
        '<div style="width: 236px;" class="list_shy">'+
        '<div class="ani_imgBox"><img src="'+detail[i].poster+'" style="width: 236px;height: 144px;" alt=""></div>'+
        '</div>'+
        '<div class="right-box">'+
            '<div class="expertDetail-title-box">'+
                '<div class="title">'+detail[i].title+'</div>'+
            '</div>'+
            '<div class="expertDetail-constent">'+detail[i].description+'</div>'+
            '<div class="list-bottom-box">'+
                '<div>'+detail[i].createTime+'</div>'+
                '<div class="time-box">'+
                    '<img src="images/expertDetail/fans-2.png" style="width: 16px;height: 16px;margin-right: 5px;">'+
                    '<div>'+detail[i].commentCount+'</div>'+
                '</div>'+
            '</div>'+
          '</div>'+
        '</div>'
      }
      $("#mainList").html(mainListHtml);
    }else if(categoryId==36){ //今日焦点
      var html='';
      for(var i=0;i<detail.length;i++){
        var time=timeUplc(detail[i].createTime);
        html+='<div class="cont-box ww-box" onclick="goLessonDetail(' + detail[i].contentId + ',\'\u7ECF\u6D4E\',\''+name+'\')">'+
        '<div class="list-box list-box-rest">'+
            '<div class="right-box right-box-reset">'+
                '<div class="expertDetail-constent">'+detail[i].description+'</div>'+
                '<div class="list-bottom-box">'+
                  '<div class="time-box">'+
                      // '<img src="images/expertDetail/fans-2.png" style="width: 16px;height: 16px;margin-right: 5px;">'+
                      '<div>'+detail[i].title+'</div>'+
                  '</div>'+
                  '<div>'+time+'</div>'+
                '</div>'+
              '</div>'+
            '</div>'+
        '</div>'
      }
      $("#jd_box").html(html);
      roll(50);
    }else if(categoryId==35){//保险知识
      var html='';
      for(var i=0;i<detail.length;i++){
        html+=' <div class="cont-box cont-box-l cont-box-last" onclick="goLessonDetail(' + detail[i].contentId + ',\'\u7ECF\u6D4E\',\''+name+'\')">'+
          '<div class="word-industry-last">'+detail[i].title+'</div>'+
        '</div>'
      }
      $("#bxzstj_box").after(html);
    }

  });
}

function timeUplc(time){
  var time1=new Date(time)/1000;
  var time=new Date()/1000;
  if(time<time1){ return 0 +"分钟前"}
  else {
    if(time-time1>24*60*60){//大于一天
      var day=parseInt((time-time1)/(24*60*60));
      return day+"天前";
    }else if(time-time1>3600){
      var hour = parseInt((time-time1)/(60*60));
      return hour+"小时前";
    }else{
      var miniour=parseInt((time-time1)/(60));
      return miniour+"分钟前";
    }
  }
}
/**
 * 页面初始化时加载 window.onload
 */
$(function () {
  //调用server加载首页新闻
  posterImg();
  // topic();
  realTinfo();
  // financeAndEconomics();
  // researchFindings();
  getNewsData();
  $("#topicContent .topic-title").click(function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(e);
    if (index == 1) {} else if (index == 2) {} else if (index == 3) {}
  });

  $("#researchFindings .topic-title").click(function (e) {
    var index = e.currentTarget.dataset.index;
    if (index == 1) {} else if (index == 2) {} else if (index == 3) {}
  });
   
});


function roll(t) {
  var ul1 = document.getElementById("jd_box");
  var ul2 = document.getElementById("jd_box1");
  var ulbox = document.getElementById("jd_hotNewsBox");
  ul2.innerHTML = ul1.innerHTML;
  ulbox.scrollTop = 0; // 开始无滚动时设为0
  var timer = setInterval(rollStart, t); // 设置定时器，参数t用在这为间隔时间（单位毫秒），参数t越小，滚动速度越快
  // 鼠标移入div时暂停滚动
  ulbox.onmouseover = function () {
    clearInterval(timer);
  }
  // 鼠标移出div后继续滚动
  ulbox.onmouseout = function () {
    timer = setInterval(rollStart, t);
  }
}
// 开始滚动函数
function rollStart() {
  // 上面声明的DOM对象为局部对象需要再次声明
  var ul1 = document.getElementById("jd_box");
  var ul2 = document.getElementById("jd_box1");
  var ulbox = document.getElementById("jd_hotNewsBox");
  // 正常滚动不断给scrollTop的值+1,当滚动高度大于列表内容高度时恢复为0
  if (ulbox.scrollTop >= ul1.scrollHeight) {
    ulbox.scrollTop = 0;
  } else {
    ulbox.scrollTop++;
  }
}
